![DynamoDB Table Creation: With CDK and TypeScript](https://devio2023-media.developers.io/wp-content/uploads/2022/08/amazon-dynamodb.png)
DynamoDB Table Creation: With CDK and TypeScript
In this blog, we'll create a simple DynamoDB table using the power of CDK and TypeScript. But before that, let's know about CDK, the Cloud Development Kit. It's an open-source framework that empowers developers to define their desired cloud resources within the familiar comfort of their preferred programming language. CDK offers a higher level of abstraction, allowing us to write concise and maintainable code for better infrastructure.
And the best part? It support for a wide range of programming languages, including TypeScript, JavaScript, Python, Java, C#, .Net, and Go. This flexibility ensures developer can find their comfort zone within the CDK framework.
Dynamodb in CDK
import { Duration, RemovalPolicy, Stack, StackProps } from "aws-cdk-lib"; import * as dynamodb from "aws-cdk-lib/aws-dynamodb"; import { Construct } from "constructs"; export class CdkCreateDynamoDbStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); const table = new dynamodb.Table(this, "dynamocdktable", { tableName: "dynamocdktable", partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING, }, sortKey:{ name:"sk", type:dynamodb.AttributeType.STRING, }, billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, deletionProtection: true, pointInTimeRecovery: true, removalPolicy: RemovalPolicy.RETAIN, }); } }
1.Initiating a project.
- create a new empty directory.
mkdir cdk_create_dynamo_db && cd cdk_create_dynamo_db
- run command
cdk init sample-app --language typescript
to create a typescript project.
2.Constructing the DynamoDB Table:
- Stack Creation: The
CdkCreateDynamoDbStack
class extendsStack
, a fundamental CDK construct representing a collection of AWS resources. - Table Construction: The
new dynamodb.Table(...)
line creates a DynamoDB table with specified properties.
3. Defining Table Properties:
- tableName: Sets the name of the table.
- partitionKey: Defines the primary key (required for every DynamoDB table), using a string type in this case.
- sortKey: Specifies an optional secondary key to enable more complex queries and sorting.
- billingMode: Sets the billing mode to PAY_PER_REQUEST, meaning you pay only for the reads and writes you perform also there are other billing Modes.
- deletionProtection: Prevents accidental deletion of the table.
- pointInTimeRecovery: Enables point-in-time recovery, allowing you to restore the table to a previous state in case of data loss or errors.
- removalPolicy: Specifies that the table should be retained even if the stack is deleted, ensuring data preservation.
- There are numerous additional properties that can be included in the table, apart from the mandatory partition key. While everything else is optional, these additional properties allow us to provide more specificity to the table.
4. Deploy:
- cdk bootstrap : Run
cdk bootstrap
which deploys the CDK toolkit stack into an AWS environment. - cdk deploy : Run
cdk deploy
which synthesizes app's stacks before deploying to make sure that the deployment reflects the latest version of the app.
Result:
Navigate to the 'dynamodb/tables' section in the AWS Management Console. Once there, you'll find the table that you've successfully created using the AWS Cloud Development Kit (CDK).
Happy coding !
References
https://cdkworkshop.com/20-typescript/20-create-project/100-cdk-init.html
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_dynamodb.Table.html#example